package com.UnionPayCenter.wechat;

import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @program: UnionPayServers
 * @description: 微信调用接口httpClient
 * @author: bite
 * @create: 2019-07-21 16:19
 **/
@Slf4j
public class WxHttpClient {

    public static String post(String urlStr, String params) throws Exception {
        String result = "";
        try {
            //建立http连接
            URL url = new URL(urlStr);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setUseCaches(false);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.connect();
            //发送数据
            if(!StringUtils.isNullStr(params)) {
                DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
                out.write(params.getBytes("utf-8"));
                out.flush();
                out.close();
            }
            //读取数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            StringBuffer buffer = new StringBuffer();
            while ((line=reader.readLine())!=null) {
                buffer.append(line);
            }
            result = buffer.toString();
            urlConnection.disconnect();
            reader.close();

//            log.info(String.format("WxHttpClient post url: %s, return: %s", urlStr, result));
            return result;
        } catch (Exception e) {
            throw e;
        }
    }

    public static String get(String url) throws Exception {
        HttpGet httpGet = new HttpGet(url);
        // 生成一个Http客户端对象
        HttpClient httpClient = new DefaultHttpClient();
        // 下面使用Http客户端发送请求，并获取响应内容
        InputStream inputStream = null;
        // 发送请求并获得响应对象
        HttpResponse mHttpResponse = null;

        BufferedReader bufferedReader = null;
        String result = "";
        String line;
        try {
            mHttpResponse = httpClient.execute(httpGet);
            // 获得响应的消息实体
            HttpEntity mHttpEntity = mHttpResponse.getEntity();

            // 获取一个输入流
            inputStream = mHttpEntity.getContent();
            bufferedReader = new BufferedReader(
                    new InputStreamReader(inputStream));
            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }
//            log.info(String.format("WxHttpClient get url: %s, return: %s", url, result));
            return result;
        } catch (IOException e) {
            throw e;
        } finally {
            bufferedReader.close();
            inputStream.close();
            httpGet.clone();
        }
    }
}
